//
//  UserCenterHomeTableHeaderView.m
//  much
//
//  Created by Believer Just on 2019/1/8.
//  Copyright © 2019年 iOS. All rights reserved.
//

#import "UserCenterHomeTableHeaderView.h"

@interface UserCenterHomeTableHeaderView ()

/** 设置按钮图片 */
@property (nonatomic, strong) UIImageView *settingImageView;
/** 用户头像 */
@property (nonatomic, strong) UIImageView *userHeaderImageView;
/** 用户名 */
@property (nonatomic, strong) UILabel *userNameLabel;

@end

@implementation UserCenterHomeTableHeaderView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        [self initView];
    }
    return self;
}


#pragma mark - method
- (void)initView {
    self.settingImageView.hidden = NO;
    
    if (!USER.isLogin) {
        [self.userHeaderImageView sd_setImageWithURL:[NSURL URLWithString:@""] placeholderImage:IMAGE_NAME(@"user_home_userHeader")];
        self.userNameLabel.text = @"登录/注册";
    } else {
        [self.userHeaderImageView sd_setImageWithURL:[NSURL URLWithString:USER.userBaseInfo.infoModel.avatarImagePath] placeholderImage:IMAGE_NAME(@"user_home_userHeader")];
        self.userNameLabel.text = [NSString backDefaultIfNilWithString:USER.userBaseInfo.infoModel.nickname defaultString:@""];
    }
}
- (void)clickView {
    if (self.userCenterHomeTableHVBlock) {
        self.userCenterHomeTableHVBlock();
    }
}


#pragma mark - setting
- (void)setUploadValue:(BOOL)uploadValue {
    _uploadValue = uploadValue;
    
    if (!USER.isLogin) {
        [self.userHeaderImageView sd_setImageWithURL:[NSURL URLWithString:@""] placeholderImage:IMAGE_NAME(@"user_home_userHeader")];
        self.userNameLabel.text = @"登录/注册";
    } else {
        [self.userHeaderImageView sd_setImageWithURL:[NSURL URLWithString:USER.userBaseInfo.infoModel.avatarImagePath] placeholderImage:IMAGE_NAME(@"user_home_userHeader")];
        self.userNameLabel.text = [NSString backDefaultIfNilWithString:USER.userBaseInfo.infoModel.nickname defaultString:@""];
    }
}


#pragma mark - 懒加载
- (UIImageView *)settingImageView {
    if (!_settingImageView) {
        _settingImageView = [UIImageView new];
        UIImage *image = IMAGE_NAME(@"user_home_setting");
        [_settingImageView setImage:image];
        [self addSubview:_settingImageView];
        [_settingImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self).with.offset(15);
            make.top.equalTo(self).with.offset(10);
            make.width.mas_equalTo(image.size.width);
            make.height.mas_equalTo(image.size.height);
        }];
        
        [_settingImageView tapUpWithBlock:^(UIView *v) {
            [self clickView];
        }];
    }
    return _settingImageView;
}
- (UIImageView *)userHeaderImageView {
    if (!_userHeaderImageView) {
        _userHeaderImageView = [UIImageView new];
        UIImage *image = IMAGE_NAME(@"user_home_userHeader");
        [_userHeaderImageView setImage:image];
        _userHeaderImageView.cornerRadius = 40;
        [self addSubview:_userHeaderImageView];
        [_userHeaderImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self);
            make.top.equalTo(self).with.offset(30);
            make.width.mas_equalTo(image.size.width);
            make.height.mas_equalTo(image.size.height);
        }];
        
        [_userHeaderImageView tapUpWithBlock:^(UIView *v) {
            [self clickView];
        }];
    }
    return _userHeaderImageView;
}
- (UILabel *)userNameLabel {
    if (!_userNameLabel) {
        _userNameLabel = [UILabel new];
        _userNameLabel.textColor = Color34;
        _userNameLabel.textAlignment = NSTextAlignmentCenter;
        _userNameLabel.font = APPFONT(17);
        [self addSubview:_userNameLabel];
        [_userNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self);
            make.top.equalTo(self.userHeaderImageView.mas_bottom).with.offset(10);
        }];
    }
    return _userNameLabel;
}

@end
